package vip.xiaonuo.common.tenant;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/2/8 09:45
 */
@Component
public class TenantHandleInterceptor implements TenantLineHandler {

    private final List<String> globalIgnoreTables = List.of("DEV_JOB", "DEV_CONFIG", "SYS_RESOURCE", "DEV_DICT", "DEV_LOG", "wechat_checkin_data", "gzx_sync_call_record", "gzx_current_id", "gzx_region_district", "gzx_access_log", "gzx_vos_access_gateway");

    @Override
    public Expression getTenantId() {
        return StpUtil.isLogin() ? new StringValue((String) StpUtil.getSession().get("tenantId")) : new NullValue();
    }

    @Override
    public boolean ignoreTable(String tableName) {
        return globalIgnoreTables.contains(tableName)
                || Boolean.FALSE.equals(TenantContext.isFilterEnabled())
                || !StpUtil.isLogin()
                || (Boolean.TRUE.equals(TenantContext.isFilterEnabled())
                && StpUtil.getRoleList().contains("superAdmin"));
    }
}
